# Maintainer: Mario Emmenlauer <memmenlauer@biodataanalysis.de>

_realname=protobuf
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.0.0
pkgrel=1
pkgdesc="Protocol Buffers - Google's data interchange format (mingw-w64)"
arch=('any')
url='https://developers.google.com/protocol-buffers/'
license=('BSD')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs" "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-cmake" "automake" "autoconf" "libtool")
options=('staticlibs' 'strip')
source=(${_realname}-${pkgver}.tar.gz::https://github.com/google/protobuf/archive/v${pkgver}.tar.gz
        googlemock-release-1.7.0.tar.gz::https://github.com/google/googlemock/archive/release-1.7.0.tar.gz
	googletest-release-1.7.0.tar.gz::https://github.com/google/googletest/archive/release-1.7.0.tar.gz
	0001-protobuf-3.0.0-gcc6.1.0-tests.patch)
sha256sums=('f5b3563f118f1d3d6e001705fa7082e8fc3bda50038ac3dff787650795734146'
            '3f20b6acb37e5a98e8c4518165711e3e35d47deb6cdb5a4dd4566563b5efd232'
	    'f73a6546fdf9fce9ff93a5015e0333a8af3062a152a9ad6bcb772c96687016cc'
	    'be0f40a2b909435c89da3be68aa21dfe48c89b86bbc758bb81ac6aeee6522637')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  mv ../googlemock-release-1.7.0 ./gmock
  mv ../googletest-release-1.7.0 ./gmock/gtest

  patch -p1 -i ${srcdir}/0001-protobuf-3.0.0-gcc6.1.0-tests.patch
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  cp -rf "${_realname}-${pkgver}" "build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  CPPFLAGS+=" -D_WIN32_WINNT=0x0601"

  # autogen and configure are used to generate the pkg-config files
  # that will be manually installed. The cmake-based build does not
  # create pkg-config files. Remove autogen and configure if pkg-config
  # is not required:
  ./autogen.sh
  ./configure \
      --prefix="${MINGW_PREFIX}" \
      --build="${MINGW_CHOST}" \
      --host="${MINGW_CHOST}" \
      --enable-shared

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake ./cmake \
      -Wno-dev \
      -G"MSYS Makefiles" \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_BUILD_TYPE="Release" \
      -DCMAKE_SKIP_RPATH="ON" \
      -Dprotobuf_MSVC_STATIC_RUNTIME="ON" \
      -DBUILD_SHARED_LIBS="ON" \
      -Dprotobuf_BUILD_TESTS="ON" \
      -Dprotobuf_WITH_ZLIB="ON"

  make -j1
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make -j1 check
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}/${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
  install -Dm644 "${srcdir}/build-${MINGW_CHOST}/protobuf.pc" "${pkgdir}/${MINGW_PREFIX}/lib/pkgconfig/protobuf.pc"
  install -Dm644 "${srcdir}/build-${MINGW_CHOST}/protobuf-lite.pc" "${pkgdir}/${MINGW_PREFIX}/lib/pkgconfig/protobuf-lite.pc"
}
